package com.yazi.mxz.gather.main.goods;

import java.util.List;

import com.myframework.smhj.common.tool.DateUtil;
import com.yazi.mxz.gather.common.App;
import com.yazi.mxz.gather.entity.beans.StoreGoods;
import com.yazi.mxz.gather.service.StoreGoodsService;
import com.yazi.mxz.gather.service.StoreService;

/**
 * 更新商品
 * @author mo_yq5
 * @date 2015-8-17
 */
public class StoreGoodsUpdate extends Thread {
	
	private StoreGoodsService storeGoodsService;
	private StoreService storeService;
	
	
	public StoreGoodsUpdate() {
		storeGoodsService = App.context.getBean(StoreGoodsService.class);
		storeService = App.context.getBean(StoreService.class);
	}

	@Override
	public void run() {
		com.yazi.mxz.gather.entity.beans.Store store  = new com.yazi.mxz.gather.entity.beans.Store();
		store.setCompanyId(1l);
		List<com.yazi.mxz.gather.entity.beans.Store> list = storeService.list(store);
		List<StoreGoods> goodsList;
		StoreGoods storeGoods;
		Float price;
		String unit;
		int index;
		for (com.yazi.mxz.gather.entity.beans.Store storeOrc : list) {
			System.out.println("开始更新: " + storeOrc.getName());
			storeGoods = new StoreGoods();
			storeGoods.setStore(storeOrc);
			goodsList = storeGoodsService.list(storeGoods);
			for (StoreGoods sg : goodsList) {
				price = sg.getPrice();
				unit = sg.getUnit();// 200gX12瓶/箱 或 200gx12瓶/箱 改为 瓶
				if ((index = unit.indexOf("X")) > -1 || (index = unit.indexOf("x")) > -1 ) {
					if (unit.lastIndexOf("/") > index) {
						index = unit.lastIndexOf("/");
						unit = unit.substring(index - 1, index);
						if (unit.matches("[\u0391-\uFFE5]+")) {
							sg.setUnit(unit);
						}
					}
				}
				sg.setAtDiscount(Math.random() > 0.5 ? true : false);
				sg.setAtDiscountLocal(Math.random() > 0.5 ? true : false);
				sg.setAtGift(Math.random() > 0.5 ? true : false);
				sg.setAtGiftLocal(Math.random() > 0.5 ? true : false);
				sg.setDealAll(new Double(Math.random() * 999).intValue());
				sg.setDealMonth(new Double(Math.random() * 99).intValue());
				sg.setDealTime(DateUtil.getTimeStamp());
				sg.setDealWeek(new Double(Math.random() * 9).intValue());
				sg.setOriginalPrice(Float.parseFloat(String.format("%.1f0", price + price * 0.02f)));
				sg.setPrice(Float.parseFloat(String.format("%.1f0", price)));
				sg.setSelfCost(Float.parseFloat(String.format("%.1f0", price)));
				sg.setSort(new Double(Math.random() * DateUtil.getTimeStamp()).longValue());
				sg.setStock((float)(new Double(Math.random()*99).intValue()));
				sg.setUpdateTime(DateUtil.getTimeStamp());
				storeGoodsService.merge(sg);
				System.out.println("更新商品: " + sg.getName());
			}
			
			storeOrc.setLat((Math.random()*(2280-2277) + 2279)/100);
			storeOrc.setLng((Math.random()*(10837-10836) + 10836)/100);
			storeOrc.setRecommandSort(new Double(Math.random() * 999999).longValue());
			storeOrc.setUpdateTime(DateUtil.getTimeStamp());
			storeService.merge(storeOrc);
			System.out.println("更新结束: " + storeOrc.getName());
			
		}
		
	}
	
}
